package main

import (
	"errors"
	"fmt"
	"strings"
)

/*leetcode 203题 移除链表元素*/

type ListNode struct {
	Val  int
	Next *ListNode
}

func (l *ListNode) String() string {
	sb := strings.Builder{}
	cur := l
	for cur != nil {
		sb.WriteString(fmt.Sprintf("%v->", cur.Val))
		cur = cur.Next
	}
	sb.WriteString("NULL")
	return sb.String()
}

func MakeListNode(arr ...int) *ListNode {
	if len(arr) == 0 {
		panic(errors.New("arr can not be empty"))
	}
	head := &ListNode{Val: arr[0]}
	list := head
	for i := 1; i < len(arr); i++ {
		list.Next = &ListNode{Val: arr[i]}
		list = list.Next
	}
	return head
}

func removeElements(head *ListNode, val int) *ListNode {
	list := &ListNode{Next: head}
	for pre := list; pre.Next != nil; {
		cur := pre.Next
		if cur.Val == val {
			pre.Next = cur.Next
			cur.Next = nil
		} else {
			pre = pre.Next
		}
	}
	return list.Next
}

func sum(arr ...int) int {
	if len(arr) == 1 {
		return arr[0]
	}
	return arr[0] + sum(arr[1:len(arr)]...)
}

func main() {
	//list := &ListNode{Val: 1, Next: &ListNode{Val: 2,
	//	Next: &ListNode{Val: 6,
	//		Next: &ListNode{Val: 3,
	//			Next: &ListNode{Val: 4,
	//				Next: &ListNode{Val: 5,
	//					Next: &ListNode{Val: 6,
	//						Next: nil}}}}}}}
	//var list *ListNode = nil

	//list := &ListNode{Val: 7,
	//	Next: &ListNode{Val: 7,
	//		Next: &ListNode{Val: 7,
	//			Next: &ListNode{Val: 6,
	//				Next: &ListNode{Val: 7,
	//					Next: &ListNode{Val: 7,
	//						Next: nil}}}}}}

	list := MakeListNode(7, 7, 7, 6, 7, 7)
	fmt.Println(list)
	list0 := removeElements(list, 7)
	fmt.Println(list0)

	fmt.Println(sum(1, 2, 3, 4, 5, 6, 7, 8))
}
